Linux 下配置 NFS 服务

环境: Fedora & Ubuntu

安装NFS服务

yum -y install nfs-utils

对于Ubuntu则是:

apt-get install nfs-kernel-server nfs-common

以上都需要root权限.

配置NFS

编辑 /etc/exports, 添加如下内容:

/home/steffan/nfs *(insecure,rw,sync,no_root_squash)

注意星号*表示允许所有ip访问, 可以改为自己的ip. 且*后不能有空格, 带空格的话mount的时候会有错误提示mount.nfs: access denied by server while mounting

编辑 /etc/idmapd.conf
搜索并修改为Domain为自己的主机名.

启动NFS

为了方便做了一个启动NFS的脚本xNFS(适用于Fedora):

touch /usr/local/xNFS
chmod a+x /usr/local/xNFS

在xNFS脚本增加以下内容:

#!/bin/sh
echo "start service..."
systemctl start rpcbind.service
systemctl start nfs-server.service
systemctl start nfs-lock.service
systemctl start nfs-idmap.service

echo "enable service..."
systemctl enable rpcbind.service
systemctl enable nfs-server.service
systemctl enable nfs-lock.service
systemctl enable nfs-idmap.service

echo "check service status"
systemctl status rpcbind.service
systemctl status nfs-server.service
systemctl status nfs-lock.service
systemctl status nfs-idmap.service

对于Ubuntu,用下面的命令启动NFS服务:

sudo /etc/init.d/portmap start
sudo /etc/init.d/nfs-kernel-server start

测试NFS

以NFS将本地目录挂载到/mnt:

mount -t nfs localhost:/home/steffan/nfs /mnt/nfs

测试下/mnt/nfs是否能正常读写.

如果在树莓派等开发板上挂载NFS目录:

mount x.x.x.x:/home/steffan/nfs /mnt/nfs

其他内容

Fedora上可以安装图形化NFS配置工具:

yum search system-config-nfs

如果mount失败, 可以考虑暂时关闭防火墙:
开启: service iptables start
关闭: service iptables stop
或者这样关闭: /etc/init.d/iptables stop

参考:
http://www.server-world.info/en/note?os=Fedora_17&p=nfs
http://blog.csdn.net/dos5gw/article/details/5787914